package exttable.dbchecking;

import dbutils.DbUtils;
import exttable.dbclasses.DbTable;
import exttable.dbclasses.Phase;
import exttable.formgenerator.ListenerFactory;
import exttable.formgenerator.MessageFactory;
import exttable.formgenerator.StaticMethods;
import exttable.formgenerator.ViewDataBaseTableModel;
import utils.Localisation;

import javax.swing.*;
import javax.swing.border.Border;
import javax.swing.event.ListSelectionEvent;
import javax.swing.event.ListSelectionListener;
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.io.IOException;
import java.sql.ResultSet;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * Created by IntelliJ IDEA.
 * User: ADMIN
 * Date: 12.08.11
 * Time: 15:21
 * To change this template use File | Settings | File Templates.
 */
public class RunDBChecking {
    private JFrame mainFrame;
    private JPanel mainPanel;
    private SpringLayout mainSpringLayout;

    private JLabel testingLabel;
    private JScrollPane jScrollPaneTestingJList;
    private JList jListTests;
    private List<String> listOfTests;

    private JLabel phaseLabel;
    private JScrollPane jScrollPanePhaseJList;
    private JList phaseJList;
    private List<Phase> phaseList;
    public static List<Phase> phaseToReturn = new ArrayList<Phase>();
    private JCheckBox selectAllPhase;

    private JLabel medicalTestLabel;
    private JScrollPane jScrollPaneMedicalTestJList;
    private JList medicalTestJList;
    public static List<String> medicalTestToReturnList = new ArrayList<String>();
    private JCheckBox selectAllMedicalTest;
    private JButton startTest;
    private JTable warningsTable;
    private JScrollPane jScrollPaneTable;
    private ResultSet rs;

//    public static Map<String, DbTable> mapTable = StaticMethods.getMapTable(DbUtils.GENERAL_DATABASE_NAME);
    public static Map<String, DbTable> mapTable=new HashMap<String, DbTable>();

    public static Map<String, String> mapAntonymDataBaseMode;


    public RunDBChecking() {
        organizeGui();
        mapAntonymDataBaseMode = new HashMap<String, String>();
        mapAntonymDataBaseMode.put("GENERAL", "SECONDARY");
        mapAntonymDataBaseMode.put("SECONDARY", "GENERAL");
    }

    public void organizeGui() {
        mainPanel = new JPanel();
        mainPanel.setSize(1300, 800);
        Border border = BorderFactory.createLineBorder(Color.BLACK, 4);
        mainFrame = new JFrame(Localisation.getMessage("checkDBForWarnings"));
        mainFrame.setSize(1300, 800);
        mainFrame.setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);
        createTestingGui();
        createPhaseGui();
        createMedicalTestsGui();

        warningsTable = new JTable();
        jScrollPaneTable = new JScrollPane(warningsTable);
        jScrollPaneTable.setPreferredSize(new Dimension(1200, 500));
        jScrollPaneTable.setVisible(false);
        jScrollPaneTable.setBorder(border);
//        System.out.println(jScrollPaneTable.getWidth());

        startTest = new JButton(Localisation.getMessage("startTesting"));
        startTest.addActionListener(new ActionListener() {
            @Override
            public void actionPerformed(ActionEvent e) {
                phaseToReturn.clear();
                phaseToReturn = phaseJList.getSelectedValuesList();
                medicalTestToReturnList.clear();
                medicalTestToReturnList = medicalTestJList.getSelectedValuesList();
                mapTable.clear();
                if(medicalTestToReturnList.size()>0){
                    mapTable=StaticMethods.getMapSpecifiedTables(DbUtils.GENERAL_DATABASE_NAME, StaticMethods.joinStringListToString(medicalTestToReturnList));
                }

                runTesting(jListTests.getSelectedValue());
            }
        });
        mainSpringLayout = new SpringLayout();
        mainPanel.setLayout(mainSpringLayout);
        mainPanel.add(startTest);
        mainPanel.add(jScrollPaneTable);
        mainFrame.add(mainPanel);
        mainFrame.setVisible(true);
        organizeMainSpringLayout();
    }

    private void createMedicalTestsGui() {
        medicalTestLabel = new JLabel(Localisation.getMessage("tree.analysis"));
        medicalTestLabel.setVisible(false);
        medicalTestJList = new JList();
        phaseJList.addListSelectionListener(new ListSelectionListener() {
            @Override
            public void valueChanged(ListSelectionEvent e) {
                if (!phaseJList.isSelectionEmpty()) {
//                    List<Object> tempMedicalTestList = new ArrayList<Object>();
//                    for (Object phase : phaseJList.getSelectedValuesList()) {
//                        tempMedicalTestList.addAll(Phase.getTestNameListByPhase(((Phase) phase).getPhaseId()));
//                    }
//                    medicalTestJList.setListData(tempMedicalTestList.toArray());
                    String phaseString = StaticMethods.getStringForInFromString(phaseJList.getSelectedValuesList());
                    medicalTestJList.setListData(Phase.getTestNameListByPhase(phaseString).toArray());
                }

            }
        });
        jScrollPaneMedicalTestJList = new JScrollPane();
        jScrollPaneMedicalTestJList.setViewportView(medicalTestJList);
        jScrollPaneMedicalTestJList.setVisible(false);
        selectAllMedicalTest=new JCheckBox(Localisation.getMessage("label.selectAll"));
        selectAllMedicalTest.setVisible(false);
        mainPanel.add(medicalTestLabel);
        mainPanel.add(jScrollPaneMedicalTestJList);
        mainPanel.add(selectAllMedicalTest);
    }

    private void createPhaseGui() {
        phaseLabel = new JLabel(Localisation.getMessage("form.phase"));
        phaseLabel.setVisible(false);
        phaseList = Phase.getPhaseList();
        phaseJList = new JList(phaseList.toArray());
        jScrollPanePhaseJList = new JScrollPane();
        jScrollPanePhaseJList.setViewportView(phaseJList);
        jScrollPanePhaseJList.setVisible(false);

        selectAllPhase = new JCheckBox(Localisation.getMessage("label.selectAll"));
        selectAllPhase.setVisible(false);
        mainPanel.add(phaseLabel);
        mainPanel.add(jScrollPanePhaseJList);
        mainPanel.add(selectAllPhase);
    }

    private void createTestingGui() {
        testingLabel=new JLabel("testing");
        jScrollPaneTestingJList = new JScrollPane();
        listOfTests = new ArrayList<String>();
        listOfTests.add("analys_translation");
        listOfTests.add("test_parametrs_units");

        if (DbUtils.mapDataBases.get(DbUtils.SECONDARY_DATABASE_NAME) != null) {
            listOfTests.add("compare_DataBases_for_tests");
            listOfTests.add("compare_DataBases_for_questionnaire");
        }

//        for (int i = 0; i < 10; i++) {
//            listOfTests.add("analys_translation");
//            listOfTests.add("test_parametrs_units");
//
//        }
        jListTests = new JList(listOfTests.toArray());
        jListTests.setSelectionMode(ListSelectionModel.SINGLE_SELECTION);
        jListTests.addListSelectionListener(new ListSelectionListener() {
            @Override
            public void valueChanged(ListSelectionEvent e) {
                if (jListTests.getSelectedValue().toString().equals("compare_DataBases_for_tests")) {
                    phaseLabel.setVisible(true);
                    jScrollPanePhaseJList.setVisible(true);
                    selectAllPhase.setVisible(true);
                    selectAllPhase.addActionListener(ListenerFactory.selectUnSelectAllInJListWithSubJList(phaseJList, new JList[]{medicalTestJList}));

                    medicalTestLabel.setVisible(true);
                    jScrollPaneMedicalTestJList.setVisible(true);
                    selectAllMedicalTest.setVisible(true);
                    selectAllMedicalTest.addActionListener(ListenerFactory.selectUnSelectAllInJList(medicalTestJList));
                } else if (jListTests.getSelectedValue().toString().equals("compare_DataBases_for_questionnaire")) {
                    phaseLabel.setVisible(true);
                    jScrollPanePhaseJList.setVisible(true);
                    selectAllPhase.setVisible(true);
                    selectAllPhase.addActionListener(ListenerFactory.selectUnSelectAllInJList(phaseJList));

                    medicalTestLabel.setVisible(false);
                    jScrollPaneMedicalTestJList.setVisible(false);
                    selectAllMedicalTest.setVisible(false);
                } else {
                    phaseLabel.setVisible(false);
                    jScrollPanePhaseJList.setVisible(false);
                    selectAllPhase.setVisible(false);

                    medicalTestLabel.setVisible(false);
                    jScrollPaneMedicalTestJList.setVisible(false);
                    selectAllMedicalTest.setVisible(false);
                }
            }
        });
        jScrollPaneTestingJList.setViewportView(jListTests);
        mainPanel.add(testingLabel);
        mainPanel.add(jScrollPaneTestingJList);
    }

    public void organizeMainSpringLayout() {
        mainSpringLayout.putConstraint(SpringLayout.WEST, testingLabel, 100, SpringLayout.WEST, jScrollPaneTestingJList);
        mainSpringLayout.putConstraint(SpringLayout.NORTH, testingLabel, 15, SpringLayout.NORTH, mainFrame);
        mainSpringLayout.putConstraint(SpringLayout.WEST, jScrollPaneTestingJList, 15, SpringLayout.WEST, mainFrame);
        mainSpringLayout.putConstraint(SpringLayout.NORTH, jScrollPaneTestingJList, 15, SpringLayout.SOUTH, testingLabel);

        mainSpringLayout.putConstraint(SpringLayout.WEST, phaseLabel, 100, SpringLayout.WEST, jScrollPanePhaseJList);
        mainSpringLayout.putConstraint(SpringLayout.NORTH, phaseLabel, 15, SpringLayout.NORTH, mainFrame);
        mainSpringLayout.putConstraint(SpringLayout.WEST, jScrollPanePhaseJList, 15, SpringLayout.EAST, jScrollPaneTestingJList);
        mainSpringLayout.putConstraint(SpringLayout.NORTH, jScrollPanePhaseJList, 0, SpringLayout.NORTH, jScrollPaneTestingJList);
        mainSpringLayout.putConstraint(SpringLayout.WEST, selectAllPhase, 100, SpringLayout.WEST, jScrollPanePhaseJList);
        mainSpringLayout.putConstraint(SpringLayout.NORTH, selectAllPhase, 15, SpringLayout.SOUTH, jScrollPanePhaseJList);

        mainSpringLayout.putConstraint(SpringLayout.WEST, medicalTestLabel, 100, SpringLayout.WEST, jScrollPaneMedicalTestJList);
        mainSpringLayout.putConstraint(SpringLayout.NORTH, medicalTestLabel, 15, SpringLayout.NORTH, mainFrame);
        mainSpringLayout.putConstraint(SpringLayout.WEST, jScrollPaneMedicalTestJList, 15, SpringLayout.EAST, jScrollPanePhaseJList);
        mainSpringLayout.putConstraint(SpringLayout.NORTH, jScrollPaneMedicalTestJList, 0, SpringLayout.NORTH, jScrollPanePhaseJList);
        mainSpringLayout.putConstraint(SpringLayout.WEST, selectAllMedicalTest, 100, SpringLayout.WEST, jScrollPaneMedicalTestJList);
        mainSpringLayout.putConstraint(SpringLayout.NORTH, selectAllMedicalTest, 15, SpringLayout.SOUTH, jScrollPaneMedicalTestJList);

        mainSpringLayout.putConstraint(SpringLayout.WEST, startTest, 0, SpringLayout.WEST, jScrollPaneTestingJList);
        mainSpringLayout.putConstraint(SpringLayout.NORTH, startTest, 10, SpringLayout.SOUTH, jScrollPaneTestingJList);

        mainSpringLayout.putConstraint(SpringLayout.WEST, jScrollPaneTable, 15, SpringLayout.WEST, mainFrame);
        mainSpringLayout.putConstraint(SpringLayout.NORTH, jScrollPaneTable, 50, SpringLayout.SOUTH, jScrollPaneTestingJList);
    }

    public void runTesting(Object object) {
        if (object.toString().equals("compare_DataBases_for_tests")) {
            if (phaseToReturn.size() < 1|| mapTable.size()<1) {
                MessageFactory.emptyFields();
                return;
            }
            try {
                TestMethods.getThatHaveToFillForCompareExcel();
            } catch (IOException e) {
                e.printStackTrace();
            }
            mainFrame.dispose();
            return;
        } else if (object.toString().equals("compare_DataBases_for_questionnaire")) {
            if (phaseToReturn.size() < 1) {
                MessageFactory.emptyFields();
                return;
            }
            try {
                TestMethods.getThatHaveToFillToResultAnswersForCompareExcel();
            } catch (IOException e) {
                e.printStackTrace();
            }

            mainFrame.dispose();
            return;
        }
        int count = 0;
        DataBaseModelRegular dataBaseModelRegular = new DataBaseModelRegular(false);
        try {
            rs = ETestGenerator.valueOf(object.toString().toUpperCase()).getTest();
            if (rs.last()) {
                count = rs.getRow();
            }
        } catch (Exception e) {
            e.printStackTrace();
        }

        if (count < 1) {
            MessageFactory.getMessageWarnings(false);
            jScrollPaneTable.setVisible(false);
        } else {
            try {
                MessageFactory.getMessageWarnings(true);
                dataBaseModelRegular.setDataSource(ETestGenerator.valueOf(object.toString().toUpperCase()).getTest());
                warningsTable.setModel(dataBaseModelRegular);
                warningsTable.setAutoResizeMode(JTable.AUTO_RESIZE_OFF);
                ViewDataBaseTableModel.setColumnSize(warningsTable, 8);
                jScrollPaneTable.setVisible(true);
            } catch (Exception e) {
                e.printStackTrace();
            }
        }
    }

    public static void main(String args[]) {
        new RunDBChecking();
    }


}
